package com.koron.order.prodForm.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.prodForm.bean.po.ExperimentForm;
import com.koron.order.prodForm.bean.query.ExperimentFormQuery;
import com.koron.order.prodForm.service.ExperimentFormService;
import com.koron.order.prodForm.service.ProdFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "试验报表接口")
@RestController
@RequestMapping("experimenetForm")
@Slf4j
public class ExperimenetFormController implements WorkflowController {

    @Autowired
    private ExperimentFormService formService;

    @Autowired
    private WorkFlowHandler workFlowHandler;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Response<PageInfo<ExperimentForm>> page(PageQuery pageQuery, ExperimentFormQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), formService, "page", PageInfo.class, pageQuery, query));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/selectOne")
    public Response<ExperimentForm> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), formService, "getById", ExperimentForm.class, id));
    }

    @ApiOperation(value = "保存或更新数据")
    @PostMapping("/save")
    public Response<String> save(@RequestBody ExperimentForm experimentForm) {
        ADOConnection.runTask(getCurrDs(), formService, "save", Boolean.class, experimentForm);
        return Response.ok(experimentForm.getId());
    }

    @ApiOperation(value = "删除数据")
    @GetMapping("delete")
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), formService, "deleteById", Boolean.class, id);
        workFlowHandler.delete(id);
        return Response.ok();
    }

    @Override
    public Response<Object> updateState(RedirectQuery redirectQuery) {
        log.info("工作流更新: {}", JSON.toJSONString(redirectQuery));
        // 撤回或者废弃
        ExperimentForm experimentForm = new ExperimentForm();
        experimentForm.setId(redirectQuery.getDataId());
        if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            // 修改为草稿状态
            experimentForm.setStatus(ProdFormService.ProdFormStatus.DRAFT.getCode());
        } else {
            experimentForm.setStatus(ProdFormService.ProdFormStatus.WAIT_REVIEW.getCode());
        }
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), formService, "update", Boolean.class, experimentForm);
        return Response.success();
    }

    @Override
    public Response<Object> updateEnd(RedirectQuery redirectQuery) {
        log.info("工作流更新: {}", JSON.toJSONString(redirectQuery));
        // 工作流结束
        ExperimentForm experimentForm = new ExperimentForm();
        experimentForm.setId(redirectQuery.getDataId());
        experimentForm.setStatus(ProdFormService.ProdFormStatus.ARCHIVED.getCode());
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), formService, "update", Boolean.class, experimentForm);
        return Response.success();
    }

    @Override
    public Response<Object> updateCancel(RedirectQuery redirectQuery) {
        log.info("业务单: {}, 工作流节作废", JSON.toJSONString(redirectQuery));
        ExperimentForm experimentForm = new ExperimentForm();
        // 修改为废弃状态
        experimentForm.setId(redirectQuery.getDataId());
        experimentForm.setStatus(ProdFormService.ProdFormStatus.DEPRECATED.getCode());
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), formService, "update", Boolean.class, experimentForm);
        return Response.success();
    }
}
